/*
 * 
 */

package com.memorytech.starchive;

import java.util.List;
import java.util.Observable;
import java.util.Observer;
import jp.co.memory_tech.starchive.R;

import android.app.Activity;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.memorytech.starchive.db.SQLiteProvider;
import com.memorytech.starchive.httpservice.HttpClientHelper;
import com.memorytech.starchive.httpservice.AbstractMap.SimpleEntry;
import com.memorytech.starchive.httpservice.IService.Methods;
import com.memorytech.starchive.utils.SaveData;

// TODO: Auto-generated Javadoc
/**
 * The Class SettingActivity.
 * 
 * @author Tam-LT
 */
public class SettingActivity extends Activity implements OnClickListener, Observer {
    private Button setting, clear_cache;
    private HttpClientHelper httpClientHelper;
    public static final int LOGIN = 1;
    public static final int CLEAR_CACHE = 2;
    public static final int INPUT_ERROR = 3;

    /*
     * (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
        setting = (Button) findViewById(R.id.account_setting);
        setting.setOnClickListener(this);
        clear_cache = (Button) findViewById(R.id.clear_cache);
        clear_cache.setOnClickListener(this);
        TextView textView = (TextView) findViewById(R.id.title);
        textView.setText(getString(R.string.setting));
        httpClientHelper = HttpClientHelper.getInstance();
    }

    /*
     * (non-Javadoc)
     * @see android.app.Activity#onPause()
     */
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        httpClientHelper.deleteObserver(this);
    }

    /*
     * (non-Javadoc)
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.account_setting:
                String hostName = ((EditText) findViewById(R.id.setting_hostname)).getText()
                        .toString().trim();
                String username = ((EditText) findViewById(R.id.setting_username)).getText()
                        .toString().trim();
                String password = ((EditText) findViewById(R.id.setting_password)).getText()
                        .toString().trim();
                // if (hostName.equals("") || username.equals("")
                // || password.equals("")) {
                // showDialog(INPUT_ERROR);
                // return;
                // }
                Log.i("Tamle", "onClick::hostname: " + hostName);
                SaveData.getInstance(getApplicationContext()).sethostname(hostName);
                // saveConfig(hostName, username, password);
                // httpClientHelper.addObserver(this);
                // httpClientHelper.login(username, password, hostName);
                // findViewById(R.id.loading).setVisibility(View.VISIBLE);
                break;
            case R.id.clear_cache:
                showDialog(CLEAR_CACHE);
                break;
            case R.id.confirm_ok:
                clearCache();
                dismissDialog(CLEAR_CACHE);
                break;
            case R.id.confirm_cancel:
                dismissDialog(CLEAR_CACHE);
                break;
            case R.id.confirm_ok_cancel:
                dismissDialog(INPUT_ERROR);
                break;
            default:
                break;
        }
    }

    /*
     * (non-Javadoc)
     * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
     */
    @Override
    public void update(Observable observable, Object data) {
        // TODO Auto-generated method stub
        if (observable instanceof HttpClientHelper) {

            @SuppressWarnings("unchecked")
            SimpleEntry<Methods, List<Object>> ret = (SimpleEntry<Methods, List<Object>>) data;
            switch (ret.getKey()) {
                case login:
                    httpClientHelper.deleteObserver(this);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    findViewById(R.id.loading).setVisibility(View.GONE);
                    if (ret.getValue() == null || ret.getValue().size() == 0) {
                        Log.i("Tamle", "Can't login");
                    } else {
                        Log.i("Tamle", " login successfully");
                    }
                    break;

                default:
                    break;
            }

        }

    }

    /**
     * Clear cache.
     */
    public void clearCache() {
        SQLiteProvider provider = SQLiteProvider.getInstance(this, getPackageName());
        provider.clearDB();
        MainActivity activity = (MainActivity) getParent();
        activity.onClearCache();
    }

    /*
     * (non-Javadoc)
     * @see android.app.Activity#onCreateDialog(int)
     */
    @Override
    protected Dialog onCreateDialog(int id) {
        // TODO Auto-generated method stub
        Dialog dialog = null;
        switch (id) {
            case INPUT_ERROR:
                // do the work to define the INPUT_ERROR Dialog
                dialog = new Dialog(this);
                dialog.setContentView(R.layout.confirm_dialog_2);
                // set the custom dialog components - text, image and button
                TextView text = (TextView) dialog.findViewById(R.id.confirm_text);
                text.setText("Please enter full information to login");
                dialog.findViewById(R.id.confirm_ok_cancel).setOnClickListener(this);
                break;
            case CLEAR_CACHE:
                // do the work to define the CLEAR_CACHE Dialog
                dialog = new Dialog(this);
                dialog.setContentView(R.layout.confirm_dialog);
                // dialog.setTitle("Title...");

                // set the custom dialog components - text, image and button
                text = (TextView) dialog.findViewById(R.id.confirm_text);
                text.setText("Do you want to clear the cache?");
                dialog.findViewById(R.id.confirm_ok).setOnClickListener(this);
                dialog.findViewById(R.id.confirm_cancel).setOnClickListener(this);
                break;
            default:
                dialog = null;
        }
        return dialog;
    }

    public void saveConfig(String hostname, String userName, String password) {
        SharedPreferences sharedPreferences = getSharedPreferences("barcode_preference",
                MODE_PRIVATE);
        sharedPreferences.edit().putString("hostname", hostname);
        sharedPreferences.edit().putString("userName", userName);
        sharedPreferences.edit().putString("password", password);
        sharedPreferences.edit().commit();

        Log.i("Tamle", "hostname: " + sharedPreferences.getString("hostname", "hello"));
    }

}
